<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\HomeBaseController;
use think\Db;

class MeiController extends HomeBaseController{
    public function _empty($name){
        return view($name);
    }
    public function loginInfo(){
        $account = input('account');
        $password = input('password');
        $data = Db::name('mei_business')->where('account',$account)->find();
        if(!empty($data)){
            if($data['password'] == $password){
                session('user',$data);
                return ['code'=>1,'msg'=>'登录成功','data'=>$data];
            }else{
                return ['code'=>0,'msg'=>'账号密码错误，请重新输入或与管理员联系'];
            }
        }else{
            return ['code'=>0,'msg'=>'账号密码错误，请重新输入或与管理员联系'];
        }
    }
    public function infoadd(){
        $user = session('user');
        if(empty($user)){
            $this->redirect('mei/login');
        }else{
            $res = Db::table('wy_mei_shang')
                ->alias('a')
                ->join('wy_hat_province p','a.province = p.provinceID','left')
                ->join('wy_hat_city c','a.city = c.cityID','left')
                ->join('wy_hat_area r','a.area = r.areaID','left')
                ->join('wy_mei_business m','a.add_id = m.id','left')
                ->join('wy_wx_info f','m.info_id = f.id','left')
                ->where('add_id',$user['id'])
                ->field('a.*,p.provinceID,p.province,c.cityID,c.city,r.areaID,r.area,m.is_shang')
                ->find();
            $cate = Db::name('mei_cate')->select();
            $jia = Db::name('jia_cate')->select();
            $info = Db::name('wx_info')->select();
            $this->assign('res',$res);
            $this->assign('info',$info);
            $this->assign('cate',$cate);
            $this->assign('jia',$jia);
            $this->assign('user',$user);
        }
        return view();
    }
    public function jiaadd(){
        $user = session('user');
        if(empty($user)){
            $this->redirect('mei/login');
        }else{
            $res = Db::table('wy_mei_shang')
                ->alias('a')
                ->join('wy_hat_province p','a.province = p.provinceID','left')
                ->join('wy_hat_city c','a.city = c.cityID','left')
                ->join('wy_hat_area r','a.area = r.areaID','left')
                ->join('wy_mei_business m','a.add_id = m.id','left')
                ->join('wy_wx_info f','m.info_id = f.id','left')
                ->where('add_id',$user['id'])
                ->field('a.*,p.provinceID,p.province,c.cityID,c.city,r.areaID,r.area,m.is_shang')
                ->find();
            $cate = Db::name('mei_cate')->select();
            $jia = Db::name('jia_cate')->select();
            $info = Db::name('wx_info')->select();
            $cate_id = Db::name('mei_business')->where('account',$user['account'])->field('cate_id')->find();
            $this->assign('res',$res);
            $this->assign('info',$info);
            $this->assign('cate',$cate);
            $this->assign('jia',$jia);
            $this->assign('cate_id',$cate_id);
        }
        return view();
    }
    public function tuoadd(){
        $user = session('user');
        if(empty($user)){
            $this->redirect('mei/login');
        }else{
            $res = Db::table('wy_mei_shang')
                ->alias('a')
                ->join('wy_hat_province p','a.province = p.provinceID','left')
                ->join('wy_hat_city c','a.city = c.cityID','left')
                ->join('wy_hat_area r','a.area = r.areaID','left')
                ->join('wy_mei_business m','a.add_id = m.id','left')
                ->join('wy_wx_info f','m.info_id = f.id','left')
                ->where('add_id',$user['id'])
                ->field('a.*,p.provinceID,p.province,c.cityID,c.city,r.areaID,r.area,m.is_shang')
                ->find();
            $cate = Db::name('mei_cate')->select();
            $jia = Db::name('jia_cate')->select();
            $info = Db::name('wx_info')->select();
            $cate_id = Db::name('mei_business')->where('account',$user['account'])->field('cate_id')->find();
            $this->assign('res',$res);
            $this->assign('info',$info);
            $this->assign('cate',$cate);
            $this->assign('jia',$jia);
            $this->assign('cate_id',$cate_id);
        }
        return view();
    }
    public function weiadd(){
        $user = session('user');
        if(empty($user)){
            $this->redirect('mei/login');
        }else{
            $res = Db::table('wy_mei_shang')
                ->alias('a')
                ->join('wy_hat_province p','a.province = p.provinceID','left')
                ->join('wy_hat_city c','a.city = c.cityID','left')
                ->join('wy_hat_area r','a.area = r.areaID','left')
                ->join('wy_mei_business m','a.add_id = m.id','left')
                ->join('wy_wx_info f','m.info_id = f.id','left')
                ->where('add_id',$user['id'])
                ->field('a.*,p.provinceID,p.province,c.cityID,c.city,r.areaID,r.area,m.is_shang')
                ->find();
            $cate = Db::name('mei_cate')->select();
            $jia = Db::name('jia_cate')->select();
            $info = Db::name('wx_info')->select();
            $cate_id = Db::name('mei_business')->where('account',$user['account'])->field('cate_id')->find();
            $this->assign('res',$res);
            $this->assign('info',$info);
            $this->assign('cate',$cate);
            $this->assign('jia',$jia);
            $this->assign('cate_id',$cate_id);
        }
        return view();
    }
    public function infoSave(){
        $user = session('user');
        $data = input();
        $id = input('id');
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('imgsuo');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload');
            if($info){
                $data['imgsuo'] = $info->getSaveName();
            }else{
                echo $file->getError();
            }
        }
        $data['user_id'] = $user['user_id'];
        $data['add_id'] = $user['id'];
        if(!empty($id)){
            $res = Db::name('mei_shang')->where('id',$id)->update($data);
        }else{
            $res = Db::name('mei_shang')->insert($data);
        }
        if($res){
            switch ($data['type']){
                case 'mei':$this->success('保存信息成功','infoadd');break;
                case 'jia':$this->success('保存信息成功','jiaadd');break;
                case 'tuo':$this->success('保存信息成功','tuoadd');break;
                case 'wei':$this->success('保存信息成功','weiadd');break;
                default:  echo"<script>window.history.go(-1);</script>";
            }

        }
    }

//    三级联动
    public function san(){
        $provinceID = input('provinceID');
        $cityID = input('cityID');
        $provinc = Db::name('hat_province')->select();
        if(!empty($cityID)){
            $area = Db::name('hat_area')->where('father',$cityID)->select();
            return ['data'=>$area,'code'=>1,'msg'=>'区'];
        }elseif(!empty($provinceID)){
            $city = Db::name('hat_city')->where('father',$provinceID)->select();
            return ['data'=>$city,'code'=>1,'msg'=>'市'];
        }elseif(!empty($provinceChu) && !empty($cityChu) && !empty($areaChu)){

        }elseif(!empty($provinc)){
            return ['data'=>$provinc,'code'=>1,'msg'=>'省份'];
        }
    }
    


}
